
import sys
import typing

from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtWidgets import QMainWindow, QApplication, QWidget, QLabel,QMessageBox
from student_add import Ui_Form
import student_add
from utils_func import log_json,log_db
from object_class import Dormitory
import pandas as pd
import sqlite3


class Ui_Form(QWidget):
    def __init__(self):
        super(Ui_Form, self).__init__()
        self.setWindowFlag(QtCore.Qt.WindowType.MSWindowsFixedSizeDialogHint)
        self.setupUi(self)
        
        self.pushButton.clicked.connect(self.change_dorm)
        self.pushButton_2.clicked.connect(self.resetForm)
        
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(375, 274)
        self.formLayoutWidget = QtWidgets.QWidget(parent=Form)
        self.formLayoutWidget.setGeometry(QtCore.QRect(30, 60, 301, 81))
        self.formLayoutWidget.setObjectName("formLayoutWidget")
        self.formLayout = QtWidgets.QFormLayout(self.formLayoutWidget)
        self.formLayout.setContentsMargins(0, 0, 0, 0)
        self.formLayout.setObjectName("formLayout")
        self.label = QtWidgets.QLabel(parent=self.formLayoutWidget)
        self.label.setObjectName("label")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.ItemRole.LabelRole, self.label)
        self.lineEdit = QtWidgets.QLineEdit(parent=self.formLayoutWidget)
        self.lineEdit.setObjectName("lineEdit")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.ItemRole.FieldRole, self.lineEdit)
        self.label_2 = QtWidgets.QLabel(parent=self.formLayoutWidget)
        self.label_2.setObjectName("label_2")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.ItemRole.LabelRole, self.label_2)
        self.lineEdit_2 = QtWidgets.QLineEdit(parent=self.formLayoutWidget)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.ItemRole.FieldRole, self.lineEdit_2)
        self.pushButton = QtWidgets.QPushButton(parent=Form)
        self.pushButton.setGeometry(QtCore.QRect(30, 150, 111, 31))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(parent=Form)
        self.pushButton_2.setGeometry(QtCore.QRect(220, 150, 111, 31))
        self.pushButton_2.setObjectName("pushButton_2")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "更换宿舍"))
        self.label.setText(_translate("Form", "请输入学号："))
        self.label_2.setText(_translate("Form", "新宿舍："))
        self.pushButton.setText(_translate("Form", "保存"))
        self.pushButton_2.setText(_translate("Form", "取消"))
        
    def change_dorm(self):
        student_id = self.lineEdit.text().strip()
        new_dorm = self.lineEdit_2.text().strip()
        
        if not student_id or not new_dorm:
            QMessageBox.warning(self, "警告", "请输入完整信息！")
            return
        
        try:
            # 连接数据库
            conn = sqlite3.connect('student_management.db')
            cursor = conn.cursor()
            
            # 检查学生是否存在
            cursor.execute("SELECT * FROM student_info WHERE student_id = ?", (student_id,))
            student = cursor.fetchone()
            
            if not student:
                QMessageBox.warning(self, "警告", "未找到该学号的学生！")
                return
                
            # 解析新宿舍信息
            dorm_building, room = new_dorm.split('-') if '-' in new_dorm else (new_dorm, "")
            
            # 检查目标宿舍是否已满
            cursor.execute("""
                SELECT COUNT(*) 
                FROM student_info 
                WHERE dorm_building = ? AND dorm_num = ?
            """, (dorm_building, room))
            count = cursor.fetchone()[0]
            
            if count >= 4:
                QMessageBox.warning(self, "警告", "该宿舍已满，无法分配！")
                return
                
            # 更新宿舍信息
            cursor.execute("""
                UPDATE student_info 
                SET dorm_building = ?, dorm_num = ?
                WHERE student_id = ?
            """, (dorm_building, room, student_id))
            
            conn.commit()
            conn.close()
            
            QMessageBox.information(self, "成功", f"已成功将学号{student_id}的宿舍修改为{new_dorm}")
            log_db.log("管理员端","2000","更换宿舍",f"{student_id}")
            self.close()
            
        except Exception as e:
            QMessageBox.warning(self, "警告", f"发生错误:\n{str(e)}")
    
    def resetForm(self):
        self.lineEdit.clear()
        self.lineEdit_2.clear()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = Ui_Form()
    ui.show()

    sys.exit(app.exec())

            